importPackage(Packages.aurelienribon.tweenengine);
importPackage(Packages.aurelienribon.tweenengine.equations);

var Timeline=Packages.aurelienribon.tweenengine.Timeline;
var Tween=Packages.aurelienribon.tweenengine.Tween;
var Linear=Packages.aurelienribon.tweenengine.equations.Linear.INOUT;

var scheduler=new Object();
scheduler.getSize=function(mediator){
	return 1;
}

scheduler.getTimes=function(mediator){
	return [0];
}

scheduler.getParameters=function(mediator){
	return new Array();
}

scheduler.getMethods=function(mediator){
	return ["step1"];
}

scheduler.step1=function(stage){
	
	var offset=stage.getTrackOffset();
	var speed=500; // [px/s]
	
	var createTimeline=function(t,x,y,vx,vy){
		var timeline=Timeline.createSequence().beginSequence();
		timeline.pushPause(0.5);
		for(var p=0;p<10;p++){
			
			var dx=(vx<0?x-20:1000-x);
			var dy=(vy<0?y-20:280-y);
			
			var x1;var y1;var dist;
			if(dx<dy){
				// vertical bounce
				dist=Math.abs(dx);
				x1=(vx<0?20:1000);
				y1=y+vy*dist;
				vx=-vx;
			}else{
				// horizontal bounce
				dist=Math.abs(dy);
				y1=(vy<0?20:280);
				x1=x+vx*dist;
				vy=-vy;
			}
			
			
			timeline.push(Tween.to(t,1,dist/speed).target(x1,y1+offset).ease(Linear));
			
			x=x1;y=y1;
		}
		timeline.end();
		
		return timeline;
	}
	
	
	for(var i=0;i<stage.getMissileCount();i++){
		
		var initialPosition=stage.getRandomLocalPosition();
		var x0=initialPosition.x;
		var y0=initialPosition.y;
		
		// create an actor that rushes to other player
		var actor=stage.createSpecialMissileActor();
		actor.setMissileType(1);
		actor.setModelPosition(x0,y0+offset);

		// initial setup for bouncing path
		var vx=(Math.random()>0.5?-1:1);
		var vy=(Math.random()>0.5?-1:1);
		var x=x0;var y=y0;
		
		stage.tweenSpecialMissileActor(actor,createTimeline(actor,x,y,vx,vy));
		
		// create virtual actor for prediction
		var virtualActor=stage.createVirtualSpecialMissileActor(actor);
		virtualActor.setPosition(x0,y0+offset);
		stage.tweenVirtualSpecialMissileActor(virtualActor,createTimeline(virtualActor,x,y,vx,vy));
	}
	
}
